#include "merchant_module.h"
#include <iostream>

namespace TakeAwayPlatform
{
    MerchantModule::MerchantModule(DatabaseHandler& dbHandler) : dbHandler(dbHandler) {}

    bool MerchantModule::addDish(int merchant_id, const std::string& dish_name, double price, const std::string& description, const std::string& category, int stock)
    {
        std::string sql = "INSERT INTO dishes (merchant_id, name, price, description, category, stock, status) VALUES (" +
                          std::to_string(merchant_id) + ", '" + dish_name + "', " + std::to_string(price) + ", '" + description + "', '" + category + "', " +
                          std::to_string(stock) + ", 1)";
        return dbHandler.update(sql);
    }

    bool MerchantModule::updateDish(int dish_id, const std::string& dish_name, double price, const std::string& description, const std::string& category, int stock)
    {
        std::string sql = "UPDATE dishes SET name = '" + dish_name + "', price = " + std::to_string(price) + ", description = '" + description + "', category = '" +
                          category + "', stock = " + std::to_string(stock) + " WHERE dish_id = " + std::to_string(dish_id);
        return dbHandler.update(sql);
    }

    bool MerchantModule::deleteDish(int dish_id)
    {
        std::string sql = "DELETE FROM dishes WHERE dish_id = " + std::to_string(dish_id);
        return dbHandler.update(sql);
    }

    bool MerchantModule::viewDishes(int merchant_id, std::vector<std::string>& dish_names)
    {
        std::string sql = "SELECT name FROM dishes WHERE merchant_id = " + std::to_string(merchant_id);
        Json::Value result = dbHandler.query(sql);

        for (const auto& row : result)
        {
            dish_names.push_back(row["name"].asString());
        }

        return !dish_names.empty();
    }
}